local sk__meixin = fk.CreateSkill {

  name = "sk__meixin",

  tags = {},

}



sk__meixin:addEffect("active", {
  name = "sk__meixin",
  anim_type = "control",
  card_num = function()
    return Self:getMark("sk__meixin")
  end,
  target_num = 1,
  prompt = function(self,player)
    return "#sk__meixin:::" .. player:getMark("sk__meixin")
  end,
  can_use = Util.TrueFunc,
  card_filter = function(self, player, to_select, selected)
    return #selected < player:getMark("sk__meixin") and not player:prohibitDiscard(to_select)
  end,
  target_filter = function(self, player, to_select, selected, selected_cards)
    if #selected == 0 and to_select ~= player then
      local target = to_select
      return target:isMale() and target:getMark("@@sk__meixin") == 0
    end
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    room:setPlayerMark(target, "@@sk__meixin", 1)
    room:throwCard(effect.cards, sk__meixin.name, player, player)
  end,
})

sk__meixin:addEffect(fk.CardUseFinished, {
  name = "#sk__meixin_trigger",
  mute = true,
  events = { fk.CardUseFinished },
  can_trigger = function(self, event, target, player, data)
    return target == player and (player:hasSkill(sk__meixin.name) or player:isFemale()) and
        table.find(player.room.alive_players, function(p)
          return p:getMark("@@sk__meixin") > 0
        end)
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke("sk__meixin")
    if data.card.type == Card.TypeEquip then
      room:notifySkillInvoked(player, "sk__meixin", "offensive")
      for _, p in ipairs(room:getAllPlayers()) do
        if p:getMark("@@sk__meixin") > 0 and not p.dead then
          room:doIndicate(player.id, { p.id })
          room:damage({
            from = player,
            to = p,
            damage = 1,
            skillName = "sk__meixin",
          })
        end
      end
    else
      room:notifySkillInvoked(player, "sk__meixin", "control")
      for _, p in ipairs(room:getAllPlayers()) do
        if player.dead then return end
        if p:getMark("@@sk__meixin") > 0 and not p.dead then
          room:doIndicate(player.id, { p.id })
          if not p:isNude() then
            if data.card.type == Card.TypeBasic then
              local card = room:askForCardChosen(player, p, "he", "sk__meixin", "#sk__meixin-discard::" .. p.id)
              room:throwCard(card, "sk__meixin", p, player)
            elseif data.card.type == Card.TypeTrick then
              local card = room:askForCardChosen(player, p, "he", "sk__meixin", "#sk__meixin-prey::" .. p.id)
              room:moveCardTo(card, Card.PlayerHand, player, fk.ReasonPrey, "sk__meixin", nil, false, player)
            end
          end
        end
      end
    end
  end,
})
sk__meixin:addEffect(fk.StartPlayCard, {
  refresh_events = { fk.StartPlayCard, fk.TurnStart },
  can_refresh = function(self, event, target, player, data)
    if target == player then
      if event == fk.StartPlayCard then
        return player:hasSkill(sk__meixin.name, true)
      else
        return player:getMark("@@sk__meixin") > 0
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if event == fk.StartPlayCard then
      local n = #table.filter(room.alive_players, function(p)
        return p:getMark("@@sk__meixin") > 0
      end)
      room:setPlayerMark(player, "sk__meixin", n + 1)
    else
      room:setPlayerMark(player, "@@sk__meixin", 0)
    end
  end,
})
sk__meixin:addEffect(fk.TurnStart, {
  refresh_events = { fk.StartPlayCard, fk.TurnStart },
  can_refresh = function(self, event, target, player, data)
    if target == player then
     return player:getMark("@@sk__meixin") > 0
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "@@sk__meixin", 0)
  end,
})

return sk__meixin
